<template>
	<view class="content-f">
		<view class="bj">
			<view class="top253"></view>
			<view class="login-top">
				<view class="flex-column">
					<view class="flex-row">
						<view class="login-header-txt">登录</view>
						<view class="login-header-img"></view>
					</view>
					<view class="flex-column inputs">
						<view class="login-input">
							<input class="login-input-one" v-model="form.username" placeholder="请输入手机号" placeholder-class="login-input-p"/>
						</view>
					</view>
					<view class="flex-column inputs-1">
						<view class="login-input">
							<input class="login-input-one" type="password" v-model="form.password" placeholder="请输入密码" placeholder-class="login-input-p"/>
						</view>
					</view>
					<view class="flex-column inputs-1" v-show="is_register&&show_invite_mobile">
						<view class="login-input">
							<input class="login-input-one" v-model="form.invite_mobile" placeholder="请输入邀请人手机(选填)" placeholder-class="login-input-p"/>
						</view>
					</view>
					<view class="flex-row flex-content-between foot">
						<view @tap="goforgetPass" v-if="is_register==false">忘记密码</view>
						<view @tap="goCodeLogin" v-if="is_register==false">验证码登录</view>
					</view>
					<view class="flex-row flex-content-center">
						<view class="flex-column btn">
							<view v-if="is_register==false" @tap="login">登录</view>
							<view v-if="is_register==true" @tap="register">注册</view>
							<!-- #ifdef MP-WEIXIN -->
							<view>一键登录</view>
							<!-- #endif -->
							
						</view>
					</view>
					<!-- #ifdef H5 -->
					<view class="zc" v-if="is_register==false">没有账号？点击<text @tap="changeZc">注册</text></view>
					<view class="zc" v-if="is_register==true">已有账号？点击<text @tap="changeZc">登录</text></view>
					<!-- #endif -->
				
				</view>
			</view>
			<mu-suspension bottom="210" :is_position="false">
				<view class="login-foot">
					<view class="flex-row">
						<image @tap="changeYuedu" class="login-foot-img" :src="yuedu?`../../static/order/ok.png`:`../../static/order/not.png`" mode="scaleToFill"></image>
						<view class="login-foot-txt">我已阅读并同意<text @tap="gotoxieyi('user')">《服务协议》</text>和<text @tap="gotoxieyi('privacy')">《隐私政策》</text></view>
					</view>
				</view>
			</mu-suspension>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				yuedu:true,//是否已阅读协议
				is_register:false,//是否注册
				show_invite_mobile:true,//查看上级用户
				back:0,//是否直接返回
				form:{//提交的数据
					username:"",
					password:"",
					invite_mobile:"",
				}
			}
		},
		onLoad(opt) {
			if(opt.back){
				this.back = opt.back;
			}
			if(opt.is_register&&opt.is_register==1){
				this.is_register = true
			}
			if(opt.invite_mobile){
				this.form.invite_mobile = opt.invite_mobile
				this.show_invite_mobile = false;
			}
		},
		methods:{
			login(){
				let that = this;
				let form = this.form;
				if(!this.yuedu){
					this.$utils.showMessage("清先阅读协议");
					return
				}
				if(!form.username){
					this.$utils.showMessage("请输入账号或手机号");
					return
				}
				if(!form.password){
					this.$utils.showMessage("请输入密码");
					return
				}
				let data = {
					account:form.username,
					password:form.password
				}
				this.$api.login(data).then(res=>{
					if(res.userinfo){
						let userinfo = res.userinfo
						uni.setStorageSync('token',userinfo.token)
						that.$store.dispatch("getUserInfo");
						that.$utils.showMessage("登录成功",function(){
							uni.navigateBack({
								fail() {
									uni.switchTab({
										url:"/pages/tabbar/user"
									})
								}
							});
						})
					}
				})
			},
			//注册用户
			register(){
				let that = this;
				let form = this.form;
				if(!this.yuedu){
					this.$utils.showMessage("清先阅读协议");
					return
				}
				if(!form.username){
					this.$utils.showMessage("请输入账号或手机号");
					return
				}
				if(!form.password){
					this.$utils.showMessage("请输入密码");
					return
				}
				// console.log();
				this.$api.register(form).then(res=>{
					let userInfo = res.userinfo
					if(userInfo.token){
						uni.setStorageSync("token",userInfo.token)
						that.$store.dispatch("getUserInfo")
						that.$utils.showMessage("注册成功",function(){
							uni.navigateBack({
								fail() {
									uni.switchTab({
										url:"/pages/tabbar/user"
									})
								}
							});
						});
					}
				})
			},
			//跳转到协议
			gotoxieyi(index){
				uni.navigateTo({
					url:'/pages/user/agreement?type='+index
				})
			},
			//跳转到忘记密码
			goforgetPass(){
				uni.navigateTo({
					url:'/pages/user/forget_pass'
				})
			},
			//跳转到验证码登录
			goCodeLogin(){
				uni.navigateTo({
					url:'/pages/user/code_login'
				})
			},
			//修改阅读状态
			changeYuedu(){
				this.yuedu = !this.yuedu
			},
			//变更注册和登录
			changeZc(){
				this.is_register = !this.is_register
			}
		}
	}
</script>

<style lang="scss">
	.zc{
		font-size: 26rpx;
		color: #999999;
		margin-top: 20rpx;
		text-align: center;
		>text{
			color: #333333 !important;
		}
	}
	.login-foot-txt{
		font-size: 24rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
		margin-left: 12rpx;
		>text{
			color: #333333;
		}
	}
	.login-foot-img{
		width: 32rpx;
		height: 32rpx;
		margin-left: 62rpx;
	}
	.login-foot{
		width: 750rpx;
		// height: 110rpx;
		// background-color: #69D330;
	}
	.btn{
		margin-top: 120rpx;
		>view:nth-child(1){
			width: 610rpx;
			height: 88rpx;
			background: $default-button-color;
			border-radius: 48rpx 48rpx 48rpx 48rpx;
			opacity: 1;
			text-align: center;
			line-height: 88rpx;
			font-size: 32rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
		}
		>view:nth-child(2){
			width: 610rpx;
			height: 88rpx;
			background: #FFFFFF;
			border-radius: 48rpx 48rpx 48rpx 48rpx;
			opacity: 1;
			border: 1rpx solid $default-button-color;
			text-align: center;
			line-height: 88rpx;
			font-size: 32rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: $default-button-color;
			margin-top: 24rpx;
		}
	}
	.foot{
		margin-top: 32rpx;
		>view{
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
		}
	}
	.login-input-one{
		font-size: 30rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
	}
	.login-input-p{
		font-size: 30rpx;
		font-family: PingFang SC-Regular, PingFang SC;
		font-weight: 400;
		color: #999999;
	}
	.inputs{
		margin-top: 100rpx;
	}
	.inputs-1{
		margin-top: 70rpx;
	}
	.login-input{
		height: 72rpx;
		width: 610rpx;
		border-bottom: 1rpx solid #EEEEEE;
	}
	.login-header-img{
		width: 10rpx;
		height: 10rpx;
		opacity: 1;
		border: 5rpx solid #69D330;
		border-radius: 20rpx;
		margin-left: 16rpx;
	}
	.login-header-txt{
		font-size: 56rpx;
		font-family: Source Han Serif CN-Bold, Source Han Serif CN;
		font-weight: bold;
		color: #333333;
	}
	.login-top{
		margin-left: 70rpx;
		margin-right: 70rpx;
	}
	.top253{
		width: 100%;
		height: 253rpx;
	}
	.bj{
		width: 100%;
		height: 100vh;
		/* #ifdef MP */
		background-image: url('http://ws.facai.maowenke.cn/img/loginbj.png');
		/* #endif */
		/* #ifndef MP */
		background-image: url('../../static/user/loginbj.png');
		/* #endif */
		
		background-size: 100%;
	}
</style>